<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /*原型继承
        将父类的原型放到子类的原型上,基于__proto__
        子类可以重写父类方法（父类实例也会受影响）
        父类共有或私有的属性方法，最后都会变成子类的共有属性和方法
        */
      function A(x) {
        this.x = x;
      }
      A.prototype.getX = function () {
        console.log(this.x);
      };
      function B(y) {
        this.y = y;
      }
      let newA = new A(200); //把A放到B的原型链上
      B.prototype = newA;
      B.prototype.getY = function () {
        //B的原型上添加一个gety方法
        console.log(this.y);
      };
      let b1 = new B(100);
      console.log(b1.y); //100
      console.log(b1.getY()); //会去执行getY

      console.log(b1.x); //100
      console.log(b1.getX()); //会去执行getX

      newA.x = 1000; //子类更改将会影响父类
      console.log(b1.x, newA.x);

      b1.x = 2000; //父类更改不会影响子类
      console.log(b1.x, newA.x);
    </script>
  </body>
</html>
